﻿using Sys.Common;
using Sys.Model;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

namespace Sys.Core.Sys
{
    /// <summary>
    /// 用户业务
    /// </summary>
    public class UserBll : ExampleBase<SysUser, UserBll>
    {
        /// <summary>
        /// 当前用户编号
        /// </summary>
        public static string CurrentUserID
        {
            get { return HttpContext.Current.User.Identity.Name; }
        }

        /// <summary>
        /// 当前用户实体
        /// </summary>
        public static SysUser CurrentUser
        {
            get { return I.GetByKey(CurrentUserID); }
        }

        /// <summary>
        /// 是否超级管理员
        /// </summary>
        public static bool IsSuper
        {
            get { return CurrentUser.IsSuper; }
        }

        /// <summary>
        /// 登陆功能
        /// </summary>
        /// <param name="UserName">账号</param>
        /// <param name="PassWord">密码</param>
        /// <returns>结果实体</returns>
        public SysUser Login(string UserName, string PassWord)
        {
            SysLog log = new SysLog();

            log.ID = StringHelper.GetGuid();
            log.Type = "用户登陆";
            log.InsertTime = DateTime.Now;
            log.IPAddress = IPHelper.IPAddress;
            log.Table = string.Empty;
            log.UserID = string.Empty;
            log.Text = "账号:" + UserName + " 密码:" + PassWord;

            IPCheckResult icr = IPHelper.IPInfo(log.IPAddress);

            if (icr != null)
            {
                log.IPAddress += " " + icr.data.country;
                log.IPAddress += " " + icr.data.area;
                log.IPAddress += " " + icr.data.region;
                log.IPAddress += " " + icr.data.city;
                log.IPAddress += " " + icr.data.county;
                log.IPAddress += " " + icr.data.isp;
            }

            LogBll.I.Insert(log);

            PassWord = StringHelper.GetPassWord(PassWord);

            return GetEntity(m => m.UserName.Equals(UserName) && m.PassWord.Equals(PassWord));
        }
    }
}
